﻿//-----------------------------------------------------------------------
// <copyright file="MessageBoxControl.xaml.cs" company="Noboru Project">
//      Copyright (c) Noboru Project. All rights reserved.
//
//      This source code is subject to terms and conditions of the New BSD License (BSD).
//      A copy of the license can be found in the License.html file at the root of this 
//      distribution. If you can not locate the New BSD License (BSD), please send an 
//      email to noboru_adm@live.com.
//      By using this source code in any fashion, you are agreeing to be bound by
//      the terms of the New BSD License (BSD) for Noboru.
//
//      You must not remove this notice, or any other, from this software.
// </copyright>
//-----------------------------------------------------------------------

using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace Noboru.Windows.Controls
{
    /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.Class"]/*' />
    [TemplatePart(Name = "MessageBoxContentPart", Type = typeof(MessageBoxContent))]
    public partial class MessageBoxControl : Noboru.Windows.Controls.DialogControl
    {
        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.MessageBoxButtonsProperty"]/*'/>
        public static readonly DependencyProperty MessageBoxButtonsProperty = DependencyProperty.Register("MessageBoxButtons", typeof(MessageBoxButtons), typeof(MessageBoxControl), new PropertyMetadata(MessageBoxButtons.OK, MessageBoxControl.MessageBoxButtonsPropertyChangedCallback));

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.MessageBoxDefaultButtonProperty"]/*'/>
        public static readonly DependencyProperty MessageBoxDefaultButtonProperty = DependencyProperty.Register("MessageBoxDefaultButton", typeof(MessageBoxDefaultButton), typeof(MessageBoxControl), new PropertyMetadata(MessageBoxDefaultButton.Button1, MessageBoxControl.MessageBoxDefaultButtonPropertyChangedCallback));

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.MessageBoxIconProperty"]/*'/>
        public static readonly DependencyProperty MessageBoxIconProperty = DependencyProperty.Register("MessageBoxIcon", typeof(MessageBoxIcon), typeof(MessageBoxControl), new PropertyMetadata(MessageBoxIcon.None, MessageBoxControl.MessageBoxIconPropertyChangedCallback));

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.MessageProperty"]/*'/>
        public static readonly DependencyProperty MessageProperty = DependencyProperty.Register("Message", typeof(string), typeof(MessageBoxControl), new PropertyMetadata("Message", MessageBoxControl.MessagePropertyChangedCallback));

        private MessageBoxContent _messageBoxContent;

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.Constructor"]/*' />
        public MessageBoxControl()
        {
            this.InitializeComponent();
        }

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.MessageBoxDefaultButton"]/*' />
        public MessageBoxDefaultButton MessageBoxDefaultButton
        {
            get
            {
                return (MessageBoxDefaultButton)this.GetValue(MessageBoxDefaultButtonProperty);
            }

            set
            {
                this.SetValue(MessageBoxDefaultButtonProperty, value);
            }
        }

        public string Message
        {
            get
            {
                return (string)this.GetValue(MessageProperty);
            }

            set
            {
                this.SetValue(MessageProperty, value);
            }
        }

        public MessageBoxButtons MessageBoxButtons
        {
            get
            {
                return (MessageBoxButtons)this.GetValue(MessageBoxButtonsProperty);
            }

            set
            {
                this.SetValue(MessageBoxButtonsProperty, value);
            }
        }

        public MessageBoxIcon MessageBoxIcon
        {
            get
            {
                return (MessageBoxIcon)this.GetValue(MessageBoxIconProperty);
            }

            set
            {
                this.SetValue(MessageBoxIconProperty, value);
            }
        }

        private MessageBoxContent MessageBoxContent
        {
            get
            {
                return this._messageBoxContent;
            }

            set
            {
                MessageBoxContent oldMessageBoxContentPart = this._messageBoxContent;
                if (oldMessageBoxContentPart != null)
                {
                    oldMessageBoxContentPart.DialogResultChanged -= new RoutedEventHandler(this.MessageBoxContent_DialogResultChanged);
                }

                this._messageBoxContent = value;

                if (this._messageBoxContent != null)
                {
                    this._messageBoxContent.DialogResultChanged += new RoutedEventHandler(this.MessageBoxContent_DialogResultChanged);
                }
            }
        }

        /// <include file='Documentation\MessageBoxControlDocumentation.uex' path='Members/Member[@for="MessageBoxControl.OnApplyTemplate"]/*' />
        public override void OnApplyTemplate()
        {
            base.OnApplyTemplate();

            this.MessageBoxContent = (MessageBoxContent)this.FindName("MessageBoxContentPart");
            this.OnMessagePropertyChanged(new DependencyPropertyChangedEventArgs());
            this.OnMessageBoxButtonsPropertyChanged(new DependencyPropertyChangedEventArgs());
            this.OnMessageBoxIconPropertyChanged(new DependencyPropertyChangedEventArgs());
            this.OnMessageBoxDefaultButtonPropertyChanged(new DependencyPropertyChangedEventArgs());
        }

        private static void MessageBoxButtonsPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((MessageBoxControl)sender).OnMessageBoxButtonsPropertyChanged(e);
        }

        private static void MessageBoxDefaultButtonPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((MessageBoxControl)sender).OnMessageBoxDefaultButtonPropertyChanged(e);
        }

        private static void MessageBoxIconPropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((MessageBoxControl)sender).OnMessageBoxIconPropertyChanged(e);
        }

        private static void MessagePropertyChangedCallback(DependencyObject sender, DependencyPropertyChangedEventArgs e)
        {
            ((MessageBoxControl)sender).OnMessagePropertyChanged(e);
        }

        private void MessageBoxContent_DialogResultChanged(object sender, RoutedEventArgs e)
        {
            this.DialogResult = ((MessageBoxContent)sender).DialogResult;
        }

        private void MessageBoxContent_ButtonClick(object sender, RoutedEventArgs e)
        {
            this.Close();
        }

        private void OnMessageBoxButtonsPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (this._messageBoxContent != null)
            {
                this._messageBoxContent.MessageBoxButtons = this.MessageBoxButtons;
            }
        }

        private void OnMessageBoxDefaultButtonPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (this._messageBoxContent != null)
            {
                this._messageBoxContent.MessageBoxDefaultButton = this.MessageBoxDefaultButton;
            }
        }

        private void OnMessageBoxIconPropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (this._messageBoxContent != null)
            {
                this._messageBoxContent.MessageBoxIcon = this.MessageBoxIcon;
            }
        }

        private void OnMessagePropertyChanged(DependencyPropertyChangedEventArgs e)
        {
            if (this._messageBoxContent != null)
            {
                this._messageBoxContent.Message = this.Message;
            }
        }
    }
}
